home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / script.lzh / script_0.4 / example / caller.c < prev    next >
C/C++ Source or Header  |  1996-12-17  |  3KB  |  136 lines

  1. /*
  2.    script.library
  3.    client example
  4. */
  5.  
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <rexx/rxslib.h>
  9. #include <rexx/storage.h>
  10. #include <proto/rexxsyslib.h>
  11. #include <proto/exec.h>
  12. #include <proto/dos.h>
  13.  
  14. #if defined (__SASC)
  15. #include "/script.h"
  16. #include "/script_pragmas.h"
  17. #elif defined (__GNUC__)
  18. #include "../script_inline.h"
  19. #endif
  20.  
  21. #include "example.h"
  22.  
  23. #if defined (__GNUC__)
  24. #define RXSLIB struct RxsLib
  25. #elif defined (__SASC)
  26. #define RXSLIB struct Library
  27. #endif
  28.  
  29. struct Library *ScriptBase = NULL;
  30. RXSLIB *RexxSysBase = NULL;
  31. struct RexxMsg *RexxMesg = NULL;
  32. struct MsgPort *RexxPort = NULL;
  33. struct ScriptContext *ScriptC;
  34.  
  35. int
  36. init (void)
  37. {
  38.   if (!(RexxSysBase = (RXSLIB *) OpenLibrary ("rexxsyslib.library", 36)))
  39.     return 0;
  40.   if (!(ScriptBase = OpenLibrary ("script.library", 0)))
  41.     return 0;
  42.   if (!(RexxPort = CreateMsgPort ()))
  43.     return 0;
  44.   if (!(RexxMesg = CreateRexxMsg (RexxPort, NULL, NULL)))
  45.     return 0;
  46.   if (!(ScriptC = Script_AllocContext ()))
  47.     return 0;
  48.  
  49.   Script_SetMsgContext (RexxMesg, ScriptC);
  50.   return 1;
  51. }
  52.  
  53. void
  54. clear_rexx_result (void)
  55. {
  56.   if (RexxMesg -> rm_Result2)  /* I guess this is correct :-) */
  57.   {
  58.     DeleteArgstring ((UBYTE *) RexxMesg -> rm_Result2);
  59.     RexxMesg -> rm_Result2 = 0;
  60.   }
  61. }
  62.  
  63. void
  64. cleanup (void)
  65. {
  66.   clear_rexx_result ();
  67.  
  68.   if (ScriptC)
  69.     Script_FreeContext (ScriptC);
  70.   if (RexxMesg)
  71.     DeleteRexxMsg (RexxMesg);
  72.   if (RexxPort)
  73.     DeleteMsgPort (RexxPort);
  74.   if (RexxSysBase)
  75.     CloseLibrary ((struct Library *) RexxSysBase);
  76.   if (ScriptBase)
  77.     CloseLibrary (ScriptBase);
  78. }
  79.  
  80. int
  81. main (int argc, char *argv[])
  82. {
  83.   char *port_name = PORT_NAME,
  84.        *func_name = FUNC_NAME,
  85.        *variable_name  = VAR_NAME,
  86.        *variable_value = VAR_VALUE;
  87.   struct MsgPort *port;
  88.  
  89.   atexit (cleanup);
  90.   init ();
  91.  
  92.   if (argc >= 2)
  93.     port_name = argv[1];
  94.   if (argc >= 3)
  95.     func_name = argv[2];
  96.   if (argc >= 4)
  97.     variable_name = argv[3];
  98.   if (argc >= 5)
  99.     variable_value = argv[4];
  100.  
  101.   Script_SetStringVar (ScriptC, variable_name, variable_value);
  102.  
  103.   if (!(RexxMesg -> rm_Args[0] = CreateArgstring (func_name, strlen (func_name))))
  104.     return NULL;
  105.   RexxMesg -> rm_Node.mn_Node.ln_Type = NT_MESSAGE;
  106.   RexxMesg -> rm_Node.mn_Length = sizeof (struct RexxMsg);
  107.   RexxMesg -> rm_Action = RXFUNC | RXFF_RESULT;
  108.   RexxMesg -> rm_Node.mn_ReplyPort = RexxPort;
  109.  
  110.   Forbid ();
  111.   if (!(port = FindPort (port_name)))
  112.   {
  113.     Permit ();
  114.     PutStr ("Port \"");
  115.     PutStr (port_name);
  116.     PutStr ("\" not found.\n");
  117.     return NULL;
  118.   }
  119.   PutMsg (port, (struct Message *) RexxMesg);
  120.   Permit ();
  121.   do
  122.     WaitPort (RexxPort);
  123.   while (GetMsg (RexxPort) != (struct Message *) RexxMesg);
  124.  
  125.   Script_GetStringVar (ScriptC, variable_name, &variable_value);
  126.  
  127.   PutStr ("Returned value for: \"");
  128.   PutStr (variable_name);
  129.   PutStr ("\" is \"");
  130.   PutStr (variable_value);
  131.   PutStr ("\".\n");
  132.  
  133.   ClearRexxMsg (RexxMesg, 1); /* doesn't clear rm_Result2 */
  134.   return 0;
  135. }
  136.